<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="链接id" prop="linkId">
        <el-input v-model="formData.linkId" placeholder="请输入链接id" />
      </el-form-item>
      <el-form-item label="款式编码" prop="iId">
        <el-input v-model="formData.iId" placeholder="请输入款式编码" />
      </el-form-item>
      <el-form-item label="链接名称" prop="linkName">
        <el-input v-model="formData.linkName" placeholder="请输入链接名称" />
      </el-form-item>
      <el-form-item label="用户" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入用户" />
      </el-form-item>
      <el-form-item label="店铺" prop="shopId">
        <el-input v-model="formData.shopId" placeholder="请输入店铺" />
      </el-form-item>
      <el-form-item label="销售额" prop="sales">
        <el-input v-model="formData.sales" placeholder="请输入销售额" />
      </el-form-item>
      <el-form-item label="利润" prop="profit">
        <el-input v-model="formData.profit" placeholder="请输入利润" />
      </el-form-item>
      <el-form-item label="平台" prop="platform">
        <el-input v-model="formData.platform" placeholder="请输入平台" />
      </el-form-item>
      <el-form-item label="固定值1" prop="fixedValue1">
        <el-input v-model="formData.fixedValue1" placeholder="请输入固定值1" />
      </el-form-item>
      <el-form-item label="固定值2" prop="fixedValue2">
        <el-input v-model="formData.fixedValue2" placeholder="请输入固定值2" />
      </el-form-item>
      <el-form-item label="比例值1" prop="rateValue1">
        <el-input v-model="formData.rateValue1" placeholder="请输入比例值1" />
      </el-form-item>
      <el-form-item label="比例值2" prop="rateValue2">
        <el-input v-model="formData.rateValue2" placeholder="请输入比例值2" />
      </el-form-item>
      <el-form-item label="折扣" prop="discount">
        <el-input v-model="formData.discount" placeholder="请输入折扣" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { PriceLinkApi, PriceLink } from '@/api/business/operation/price/link'

/** 商品定价-链接 表单 */
defineOptions({ name: 'PriceLinkForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  linkId: undefined,
  iId: undefined,
  linkName: undefined,
  userId: undefined,
  shopId: undefined,
  sales: undefined,
  profit: undefined,
  platform: undefined,
  fixedValue1: undefined,
  fixedValue2: undefined,
  rateValue1: undefined,
  rateValue2: undefined,
  discount: undefined,
  remark: undefined
})
const formRules = reactive({
  linkId: [{ required: true, message: '链接id不能为空', trigger: 'blur' }],
  linkName: [{ required: true, message: '链接名称不能为空', trigger: 'blur' }],
  userId: [{ required: true, message: '用户不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await PriceLinkApi.getPriceLink(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as PriceLink
    if (formType.value === 'create') {
      await PriceLinkApi.createPriceLink(data)
      message.success(t('common.createSuccess'))
    } else {
      await PriceLinkApi.updatePriceLink(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    linkId: undefined,
    iId: undefined,
    linkName: undefined,
    userId: undefined,
    shopId: undefined,
    sales: undefined,
    profit: undefined,
    platform: undefined,
    fixedValue1: undefined,
    fixedValue2: undefined,
    rateValue1: undefined,
    rateValue2: undefined,
    discount: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}
</script>
